package com.ketti.team;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

public class NewEditText extends EditText {	
	
	// these varibles for draw background
	private Paint marginPaint;
	private Paint linePaint;
	private Paint textPaint;
	private Paint lineHighLightPaint;
	
	private int paperColor;
	private int selectedLineNumber = 0;		
	
	private float margin;
	
	private boolean layoutState = false;
	
	private ArrayList<String> lineIndex = new ArrayList<String>();	
	
	// these varibles for controlling searchBox
	private LinearLayout 	searchBoxLayout;
	private EditText 		searchBoxEdit;
			
			
	
	public NewEditText(Context context) {
		super(context);
		init();
		// TODO Auto-generated constructor stub
	}
	
	public NewEditText(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
		// TODO Auto-generated constructor stub
	}	
	
	public NewEditText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init();
		// TODO Auto-generated constructor stub
	}

	public void setSearchBoxLayout(LinearLayout layout){
		searchBoxLayout = layout;
	}
	
	public void setSearchBoxEdit(EditText editText){
		searchBoxEdit = editText;
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {						
			if(event.getPointerCount() > 1){				
				if(event.getAction() == MotionEvent.ACTION_POINTER_UP){					
				if(layoutState == false){					
					searchBoxLayout.setVisibility(View.VISIBLE);
					searchBoxEdit.requestFocus();					
					layoutState = true;					
				}
				else{
					if((event.getAction() == MotionEvent.ACTION_MOVE) || (event.getAction() == MotionEvent.ACTION_POINTER_UP))
						searchBoxLayout.setVisibility(View.GONE);
					layoutState = false;
				}
			}
			
		}
		else{
			super.onTouchEvent(event);					
		}
		return true;
	}
	
	/* initialize paints for draw */
	private void init() {
		Resources myResources = getResources();
		
		marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		marginPaint.setColor(myResources.getColor(R.color.notepad_margin));
		marginPaint.setStrokeWidth(0);
		
		linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		linePaint.setColor(myResources.getColor(R.color.notepad_lines));
		linePaint.setStrokeWidth(0);
		
		textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		textPaint.setColor(myResources.getColor(R.color.notepad_text));
		textPaint.setTextAlign(Align.RIGHT);
		
		lineHighLightPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		lineHighLightPaint.setColor(myResources.getColor(R.color.line_highlight));
		
		paperColor = myResources.getColor(R.color.notepad_paper);
		margin = myResources.getDimension(R.dimen.notepad_margin);
		// TODO Auto-generated method stub
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {	
		
		int lineCount = this.getLayout().getLineCount();				
		
		canvas.drawColor(paperColor);				
		canvas.drawLine(margin-5, 0, margin-5, getMeasuredHeight(), marginPaint);
		canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint);
		
		canvas.drawRect(margin, this.getLayout().getLineBottom(selectedLineNumber) + this.getLayout().getLineAscent(selectedLineNumber), getMeasuredWidth(), this.getLayout().getLineBottom(selectedLineNumber) + this.getLayout().getLineDescent(selectedLineNumber), lineHighLightPaint);
		
		// draw lines and line number
		for(int i = 0 ; i < lineCount ; i++){
			canvas.drawLine(0, this.getLayout().getLineBottom(i) + this.getLayout().getLineDescent(i), getMeasuredWidth(), this.getLayout().getLineBottom(i) + this.getLayout().getLineDescent(i),linePaint);
			canvas.drawText(Integer.toString(i+1), 20, this.getLayout().getLineBottom(i), textPaint);			
		}		
		
		canvas.save();		
		canvas.translate(margin, 0);		
		super.onDraw(canvas);			
		canvas.restore();				
	}

	/* invoked when user change cursor */
	@Override
	protected void onSelectionChanged(int selStart, int selEnd) {
		
		if(this.getText() != null && getLayout() != null){
			getLineIndex();
			
			// when the cursor not in selection mode
			if(selStart == selEnd){
				for(String s : lineIndex){
										
					if(selStart < Integer.valueOf(s)){	
						selectedLineNumber = lineIndex.indexOf(s);						
						break;						
					}	
					
					// when the cursor is in the last line
					else if(selStart >= Integer.valueOf(lineIndex.get(lineIndex.size()-1))){
						selectedLineNumber = lineIndex.size();
						break;
					}
				}
			}
		}
		
		// redraw this view
		invalidate();		
	}
	
	/* get line index from source string */
	private void getLineIndex(){
		
		int line_index = 0;
		String source = getText().toString();
		lineIndex.clear();
		
		if(source != null){
			for(int i = 0 ; i < source.length() ; i++){
				if(source.charAt(i)=='\n'){
					lineIndex.add(line_index ++, Integer.toString(i+1));
				}
			}
		}
	}
}
